from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import xmlrpclib
import feedparser
import config

class WordPressComment:
	def __init__(self):
		self.comment_parent = 0
		self.content = ''
		self.author = ''
		self.author_url = ''
		self.author_email = ''


class update_handler(webapp.RequestHandler):
	def get(self):
		twName = config.TWITTER_NAME
		twUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='+config.TWITTER_NAME
		wpName = config.WORDPRESS_USER
		wpPass = config.WORDPRESS_PASSWD
		wpBlogId = config.WORDPRESS_BLOGID
		wpPostId = config.WORDPRESS_POSTID
		wpUrl = config.WORDPRESS_URL+'/xmlrpc.php'
		wpsrv = xmlrpclib.ServerProxy(wpUrl)
		twsrv = feedparser.parse(twUrl)
		tweets = twsrv.entries
		tweets.reverse()
		wp = WordPressComment()
		fail = 0
		for tweet in tweets:
			wp.content = tweet.title.replace(twName+':','')+'<br><hr><br>&#x21E5;WHEN:'+tweet.published+'<br>&#x21E5;VIA:'+tweet.twitter_source
			try:
				wpsrv.wp.newComment(wpBlogId,wpName,wpPass,wpPostId,wp)
			except:
				fail += 1
	




if __name__ == "__main__":
	application = webapp.WSGIApplication([('/update', update_handler)])
	run_wsgi_app(application)
	